package com.ht.modules.customerfirminfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.modules.customerfirminfo.entity.CustomerFirmInfo;
import com.ht.modules.customerfirminfo.entity.CustomerFirmInfoVo;
import com.ht.modules.customerfirminfo.mapper.CustomerFirmInfoMapper;
import com.ht.modules.customerfirminfo.service.ICustomerFirmInfoService;
import com.ht.modules.customerfirminfo.utils.CustomerFirmInfoCrawlerUtil;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  客户企业信息
 * @Author:
 * @Date: 2024-06-13
 * @Version: V1.0
 */
@Service
public class CustomerFirmInfoServiceImpl extends ServiceImpl<CustomerFirmInfoMapper, CustomerFirmInfo> implements ICustomerFirmInfoService {

    @Autowired
    private CustomerFirmInfoMapper customerFirmInfoMapper;
    @Autowired
    private CustomerFirmInfoCrawlerUtil customerFirmInfoCrawlerUtil;

    /**
     * 重写的list列表方法
     */
    @Override
    public Page<CustomerFirmInfoVo> list(Page<CustomerFirmInfoVo> page, CustomerFirmInfoVo customerFirmInfoVo, String column, String order) {
        return page.setRecords(customerFirmInfoMapper.list(page, customerFirmInfoVo, column, order));
    }

    @Override
    public List<CustomerFirmInfoVo> listNoPage(CustomerFirmInfoVo customerFirmInfoVo, String column, String order) {
        return customerFirmInfoMapper.list(null, customerFirmInfoVo, column, order);
    }

    /**
     * 新增方法
     */
    @Override
    public void add(CustomerFirmInfo customerFirmInfo) {
        String companyName = customerFirmInfo.getCompanyName();
        if (Strings.isBlank(companyName)) {
            throw new RuntimeException("请提交单位名称...");
        }
        long count = this.count(new LambdaQueryWrapper<CustomerFirmInfo>()
                .eq(CustomerFirmInfo::getDelFlag, 1)
                .eq(CustomerFirmInfo::getCompanyName, companyName)
        );
        if (count > 0) {
            throw new RuntimeException("已有该单位名称...");
        }
        customerFirmInfo.setDelFlag("0");
        this.save(customerFirmInfo);
    }

    /**
     * 编辑方法
     */
    @Override
    public void edit(CustomerFirmInfo customerFirmInfo) {
        String id = customerFirmInfo.getId();
        CustomerFirmInfo customerFirmInfoEntity = this.getById(id);
        if (customerFirmInfoEntity == null) {
            throw new RuntimeException("未找到对应实体！");
        }
        String companyName = customerFirmInfoEntity.getCompanyName();
        if (Strings.isBlank(companyName)) {
            throw new RuntimeException("请提交单位名称...");
        }
        long count = this.count(new LambdaQueryWrapper<CustomerFirmInfo>()
                .eq(CustomerFirmInfo::getDelFlag, "0")
                .eq(CustomerFirmInfo::getCompanyName, companyName)
                .ne(CustomerFirmInfo::getId, id)
        );
        if (count > 0) {
            throw new RuntimeException("已有该单位名称...");
        }
        this.updateById(customerFirmInfo);
    }

    /**
     * 删除方法（逻辑删除）
     */
    @Override
    public void delete(String id) {
        CustomerFirmInfo customerFirmInfo = this.getById(id);
        if (customerFirmInfo == null) {
            throw new RuntimeException("未找到对应实体！");
        }
        customerFirmInfo.setDelFlag("1");
        this.updateById(customerFirmInfo);
    }

    /**
     * 批量删除方法（逻辑删除）
     */
    @Override
    public void deleteBatch(String ids) {
        if (ids == null || ids.trim().isEmpty()) {
            throw new RuntimeException("参数不识别！");
        }
        customerFirmInfoMapper.updateBatchByIds("del_flag", "1", ids.split(","));
    }

    /**
     * 通过id查询方法
     */
    @Override
    public CustomerFirmInfo queryById(String id) {
        CustomerFirmInfo customerFirmInfo = this.getById(id);
        if (customerFirmInfo == null) {
            throw new RuntimeException("未找到对应实体！");
        }
        return customerFirmInfo;
    }

    /**
     * 查询企业信息
     *
     * @param customerFirmInfo 参数
     * @return 查询结果
     */
    @Override
    public List<CustomerFirmInfo> searchEnterpriseInfo(CustomerFirmInfo customerFirmInfo) {
        return customerFirmInfoCrawlerUtil.searchEnterpriseInfo(customerFirmInfo);
    }


    /**
     * 同步企业信息
     *
     * @param customerFirmInfo 参数
     */
    @Override
    public void synchronizedEnterpriseInfo(CustomerFirmInfo customerFirmInfo) {
        String companyName = customerFirmInfo.getCompanyName();
        if (Strings.isBlank(companyName)) {
            throw new RuntimeException("请提交单位名称...");
        }
        String creditWebsite = customerFirmInfo.getCreditWebsite();
        if (Strings.isBlank(creditWebsite)) {
            throw new RuntimeException("信用网址不能为空...");
        }
        CustomerFirmInfo newCustomerFirmInfo = customerFirmInfoCrawlerUtil.crawlerByCompanyName(customerFirmInfo);
        newCustomerFirmInfo.setDelFlag("0");
        long count = this.count(new LambdaQueryWrapper<CustomerFirmInfo>()
                .eq(CustomerFirmInfo::getDelFlag, 1)
                .eq(CustomerFirmInfo::getCompanyName, newCustomerFirmInfo.getCompanyName())
                .ne(Strings.isNotBlank(customerFirmInfo.getId()), CustomerFirmInfo::getId, customerFirmInfo.getId())
        );
        if (count > 0) {
            throw new RuntimeException("已有企业信息请重新同步...");
        }
        boolean saveOrUpdate = this.saveOrUpdate(newCustomerFirmInfo);
        if (!saveOrUpdate) {
            throw new RuntimeException("保存同步信息失败...");
        }
    }

}
